package com.example.questionnaires.controller;

import com.example.questionnaires.common.Result;
import com.example.questionnaires.entity.Carousel;
import com.example.questionnaires.service.CarouselService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.questionnaires.annotation.RequiresAdmin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    @GetMapping("/list")
    public Result<List<Carousel>> list() {
        List<Carousel> carousels = carouselService.list(
            new LambdaQueryWrapper<Carousel>()
                .eq(Carousel::getStatus, 1)
                .orderByAsc(Carousel::getSort)
        );
        return Result.success(carousels);
    }

    @RequiresAdmin
    @PostMapping
    public Result save(@RequestBody Carousel carousel) {
        carouselService.save(carousel);
        return Result.success();
    }

    @RequiresAdmin
    @PutMapping
    public Result update(@RequestBody Carousel carousel) {
        carouselService.updateById(carousel);
        return Result.success();
    }

    @RequiresAdmin
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        carouselService.removeById(id);
        return Result.success();
    }
} 